<?php
/**
 * Created by PhpStorm.
 * User: Mysic
 * Date: 2020/10/19
 * Time: 16:15
 */

namespace app\common\mysic\bonus;


use app\common\model\divide\Receive;
use app\common\model\Order;
use app\common\model\User;
use  app\common\model\Wxsetting;
use app\common\mysic\MysicBase;
use think\Exception;
use think\Model;

class BonusReceiveOp extends MysicBase implements BonusReceiveOpInterface
{
    const NOT_FINISHED = 0;
    const FINISHED = 1;
    const BONUS_REFUND = 1;//退款到微信
    const BONUS_REWARD = 2;//退款到赏金
    const BONUS_BALANCE = 3;//退款到零钱

    /**
     * @var Receive
     */
    protected $receiveModel;
    /**
     * @var Receive
     */
    protected $receiveEntity;
    /**
     * @var Order
     */
    protected $userOrder;
    /**
     * @var float
     */
    protected $bonus;
    /**
     * @var Wxsetting
     */
    protected $bonusSetting;
    /**
     * @var User
     */
    protected $userModel;
    protected $upperLimit;

    public function setModel($model)
    {
        if(!$model instanceof Receive || $model == null) {
            throw new Exception('设置的model为空 或 非app\common\model\divide\Receive实例');
        }
        $this->receiveModel = $model;
        return $this;
    }
    public function setUserModel(Model $userModel)
    {
        $this->userModel = $userModel;
        return $this;
    }
    public function setBonus($bonus)
    {
        $this->bonus = $bonus;
        return $this;
    }
    public function getReceiveModel()
    {
        return $this->receiveModel;
    }
    public function setUserOrder(Model $order)
    {
        $this->userOrder = $order;
        return $this;
    }
    public function getUserOrder()
    {
        return $this->userOrder;
    }
    public function setBonusSetting(Model $setting)
    {
        $this->bonusSetting = $setting;
        return $this;
    }
    public function getBonusSetting()
    {
        return $this->bonusSetting;
    }
    public function setReceiveEntity($entity)
    {
        $this->receiveEntity = $entity;
        return $this;
    }
    public function getReceiveEntity()
    {
        return $this->receiveEntity;
    }
    public function setUpperLimit($upperLimit)
    {
        $this->upperLimit = $upperLimit;
        return $this;
    }
    public function create()
    {
        $this->log->info($this->bonus, '创建订单分红队列数据时分红金额');
        $account = $this->userModel->where(['user_id' => $this->userOrder->user_id, 'wxapp_id' => $this->userOrder->wxapp_id])->value('open_id');
        $balance = bcsub(strval($this->upperLimit), strval($this->bonus), 2);
//        $isTimesAccumulate = false;
//        if($balance >= 0 && $this->bonus > 0) {
//            $isTimesAccumulate = true;
//        }
        $this->receiveModel->receives_id = null;

        $this->receiveModel->user_id = $this->userOrder->user_id;
        $this->receiveModel->account = $account;
        $this->receiveModel->order_no = $this->userOrder->order_no;
        $this->receiveModel->wxapp_id = $this->userOrder->wxapp_id;
        $this->receiveModel->payment = $this->userOrder->pay_price;
        $this->receiveModel->periods_num = $this->bonusSetting->periods_num;
        $this->receiveModel->transaction_id = $this->userOrder->transaction_id;
        $this->receiveModel->top_limit = $this->upperLimit;
        $this->receiveModel->type = 0;
        $this->receiveModel->balance =  $balance >= 0 ? $balance : $this->upperLimit;
        $this->receiveModel->total_abonus = $this->bonus;
        $this->receiveModel->times = 0;
        $this->receiveModel->reward_money = 0;
        $this->receiveModel->refund_money = 0;
        $this->receiveModel->small_change = 0;
        $this->receiveModel->is_large = 1;
        $this->receiveModel->create_time = \time();
        $this->receiveModel->update_time = \time();
        $this->receiveModel->finish_time = \time() + intval($this->bonusSetting->term) * 24 * 60 * 60;

        $this->receiveModel->isUpdate(false);
        $ret = $this->receiveModel->save();
        if(!$ret) {
            return false;
        }
        $this->receiveEntity = $this->receiveModel;
        return true;
    }
    public function update($isFinish = self::NOT_FINISHED, $bonusType = self::BONUS_REFUND)
    {
        $this->receiveEntity->times += 1;
        $this->receiveEntity->balance = $this->receiveEntity->balance - $this->bonus < 0 ? 0 : $this->receiveEntity->balance - $this->bonus;
        $this->receiveEntity->total_abonus += $this->bonus;
        $this->receiveEntity->type = $isFinish;
        $this->log->info($bonusType, '更新用户信息主表 - 分红类型');
        $this->log->info($this->bonus, '更新用户信息主表 - 分红金额');
        if($bonusType == self::BONUS_REWARD) {
            $this->receiveEntity->reward_money += $this->bonus;
        } elseif($bonusType == self::BONUS_REFUND) {
            $this->receiveEntity->refund_money += $this->bonus;
        } elseif($bonusType == self::BONUS_BALANCE) {
            $this->receiveEntity->small_change += $this->bonus;
        }
        return $this->receiveEntity->save();

    }


}